var express = require("express");
let sql = require("../conn/conn");
let jwt = require("jsonwebtoken");

const secretKey = "WQU13289SFS";
var router = express.Router();

router.get("/login", (req, res, next) => {
	let username = req.query.username;
	let pwd = req.query.password;
	let result = sql(`select * from users where username='${username}' and password='${pwd}'`);
	result.then((data) => {
		if (data.length > 0) {
			let userInfo = {
				username,
			};
			let token = jwt.sign(userInfo, secretKey, { expiresIn: 3600 * 24 });
			res.json({
				success: 1,
				msg: "登录成功",
				token: `Bearer ${token}`,
			});
		} else {
			res.json({ success: 0, msg: "登陆失败" });
		}
	});
});

module.exports = router;
